package kz.pdt12.hw.tests;

import kz.pdt12.hw.framework.ApplicationManager;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;

import java.io.File;
import java.io.FileReader;
import java.util.*;
import static kz.pdt12.hw.tests.DataGenerator.*;

/**
 * Created with IntelliJ IDEA.
 * User: raimbek
 * Date: 07.03.13
 * Time: 0:20
 */
public class TestBase {
    protected ApplicationManager app;
    private int checkFrequency;
    private int checkCounter;

    @BeforeTest
    public void setUp() throws Exception {
        String configFile = System.getProperty("configfile", "application.properties");
        Properties properties = new Properties();
        properties.load(new FileReader(new File(configFile)));
        app = new ApplicationManager(properties);
        checkCounter = 0;
        checkFrequency = Integer.parseInt(properties.getProperty("check.frequency", "0"));
    }

    @AfterTest
    public void tearDown() throws Exception {
        app.stop();
    }

    @DataProvider
    public Iterator<Object[]> randomValidGroupGenerator() {
        return wrapObjectsForDataProvider(generateRandomGroups(5)).iterator();
    }

    @DataProvider
    public Iterator<Object[]> randomValidContactGenerator() {
        return wrapObjectsForDataProvider(generateRandomContacts(5)).iterator();
    }

    protected boolean wantToCheck() {
        checkCounter++;
        if (checkCounter > checkFrequency) {
            checkCounter = 0;
            return true;
        } else {
            return false;
        }
    }
}
